package cn.leonis.leetcode;

/**
 * @Author Tobi
 * @Date 2021-07-16 下午2:53
 * 赎金信
 */
public class RansomNoteSolution {

    public static void main(String[] args) {
        String ransomNote = "aa";
        String magazine = "ab";
        RansomNoteSolution solution = new RansomNoteSolution();
        System.out.println(solution.canConstruct(ransomNote, magazine));
    }

    public boolean canConstruct(String ransomNote, String magazine) {
        if (magazine.length() < ransomNote.length()) {
            return false;
        }

        int[] caps = new int[26];
        for (char c : ransomNote.toCharArray()) {
            int index = magazine.indexOf(c, caps[c - 'a']);
            if (index == -1) {
                return false;
            }
            caps[c - 97] = index + 1;
        }

        return true;
    }
}
